package club.xcgame520.picturepicker.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import club.xcgame520.picturepicker.R;
import club.xcgame520.picturepicker.bean.Check;
import club.xcgame520.picturepicker.bean.PictureFolder;
import club.xcgame520.picturepicker.util.ScreenUtil;

import java.io.File;
import java.util.List;

/**
 * 描述
 * <br/>
 * Created by 小灿 on 2016/12/6.
 */

public class FolderWindow extends PopupWindow {

    private Context context;

    private List<PictureFolder> pictureFolderList;
    private List<Check> checkList;

    private RecyclerView rv_content;
    private FolderAdapter adapter;

    private boolean isDismiss = false;

    private OnItemClickListener listener;

    public FolderWindow(Context context) {
        super(context);

        this.context = context;

        View contentView = LayoutInflater.from(context).inflate(R.layout.view_window_folder, null);
        this.setContentView(contentView);
        this.setWidth(ScreenUtil.getScreenWidth(context));
        this.setHeight(ScreenUtil.getScreenHeight(context) - ScreenUtil.getStatusBarHeight(context) - ScreenUtil.dip2px(context, 100));
        this.setAnimationStyle(R.style.WindowStyle);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(Color.argb(153, 0, 0, 0)));

        adapter = new FolderAdapter();
        rv_content = contentView.findViewById(R.id.rvContent);
        rv_content.setLayoutManager(new LinearLayoutManager(context));
        rv_content.setAdapter(adapter);
    }

    public void bindData(List<PictureFolder> pictureFolderList, PictureFolder pictureFolder){
        this.pictureFolderList = pictureFolderList;
        checkList = Check.createCheck(pictureFolderList.size());
        checkList.get(pictureFolderList.indexOf(pictureFolder)).setCheck(true);
        adapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.up_in);
        rv_content.startAnimation(animation);
    }

    @Override
    public void dismiss() {
        if (isDismiss) {
            return;
        }
        isDismiss = true;
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.down_out);
        rv_content.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isDismiss = false;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        FolderWindow.super.dismiss();
                    }
                }, 10);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private class FolderAdapter extends RecyclerView.Adapter<FolderAdapter.ViewHolder> {

        class ViewHolder extends RecyclerView.ViewHolder {

            LinearLayout ll_item;
            ImageView iv_firstPicture;
            TextView tv_folderName;
            TextView tv_pictureNumber;
            RadioButton rb_selected;

            public ViewHolder(View itemView) {
                super(itemView);

                ll_item = itemView.findViewById(R.id.llItem);
                iv_firstPicture = itemView.findViewById(R.id.ivFirstPicture);
                tv_folderName = itemView.findViewById(R.id.tvFolderName);
                tv_pictureNumber = itemView.findViewById(R.id.tvPictureNumber);
                rb_selected = itemView.findViewById(R.id.rbSelected);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.view_item_folder, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {

            PictureFolder pictureFolder = pictureFolderList.get(position);

            Glide.with(context)
                    .load(new File(pictureFolder.getFirstPicturePath()))
                    .apply(new RequestOptions().centerCrop().placeholder(R.mipmap.pp_placeholder).error(R.mipmap.pp_placeholder).dontAnimate())
                    .into(holder.iv_firstPicture);

            holder.tv_folderName.setText(pictureFolder.getName());
            holder.tv_pictureNumber.setText(pictureFolder.getPictureNumber() + " 张");
            holder.rb_selected.setChecked(checkList.get(position).isCheck());

            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                    Check.singleCheck(checkList, position, true);
                    adapter.notifyDataSetChanged();
                    dismiss();
                }
            });
        }

        @Override
        public void onViewRecycled(ViewHolder holder) {
            super.onViewRecycled(holder);
            holder.rb_selected.setOnCheckedChangeListener(null);
        }

        @Override
        public int getItemCount() {
            if (pictureFolderList == null) {
                return 0;
            }
            return pictureFolderList.size();
        }

    }
}
